﻿using System;
using System.Collections.Generic;
using System.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public partial class InfoResource : ICreator { }




	/// <summary>
	/// InfoResources 类。
	/// </summary>
	public static partial class InfoResources
	{
		public const string INFO_TYPE = "InfoResource";



		#region GetItem
		/// <summary>
		/// 根据标识获取指定的信息资源。
		/// </summary>
		/// <param name="o">信息资源集合。</param>
		/// <param name="resourceId">获取的信息资源标识 ResourceId。</param>
		/// <returns>获取的信息项。</returns>
		public static InfoResource GetItem(int resourceId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.InfoResources.GetItem(resourceId);
		}

		/// <summary>
		/// 根据标识获取指定的信息资源。
		/// </summary>
		/// <param name="o">信息资源集合。</param>
		/// <param name="resourceId">获取的信息资源标识 ResourceId。</param>
		/// <returns>获取的信息项。</returns>
		public static InfoResource GetItem(this System.Data.Linq.Table<InfoResource> o, int resourceId)
		{
			return o.SingleOrDefault(p => p.ResourceId == resourceId);
		}




		/// <summary>
		/// 根据标识获取指定的信息资源水印配置。
		/// </summary>
		/// <param name="o">信息资源水印集合。</param>
		/// <param name="resourceId">获取的信息资源标识 ResourceId。</param>
		/// <returns>获取的水印配置。</returns>
		public static InfoResourceWatermark GetItemWatermark(this System.Data.Linq.Table<InfoResourceWatermark> o, int resourceId)
		{
			return o.SingleOrDefault(p => p.ResourceId == resourceId);
		}

		/// <summary>
		/// 根据标识获取指定的信息资源水印配置。
		/// </summary>
		/// <param name="resourceId">获取的信息资源标识 ResourceId。</param>
		/// <returns>获取的水印配置。</returns>
		public static InfoResourceWatermark GetItemWatermark(int resourceId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.InfoResourceWatermarks.GetItemWatermark(resourceId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的信息资源是否已经存在。
		/// </summary>
		/// <param name="o">信息资源集合。</param>
		/// <param name="resourceId">验证的信息资源标识 ResourceId。</param>
		/// <returns>布尔值表示指定的信息资源是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<InfoResource> o, int resourceId)
		{
			return o.Any(p => p.ResourceId == resourceId);
		}
		#endregion
	}



	/// <summary>
	/// InfoResources 类操作部分。
	/// </summary>
	public static partial class InfoResources
	{
		#region Insert
		/// <summary>
		/// 添加信息资源。
		/// </summary>
		/// <param name="insertedResource">添加的信息资源。</param>
		/// <param name="infoType">信息类别。</param>
		/// <returns>添加的信息资源。</returns>
		public static InfoResource Insert(InfoResource insertedResource, string infoType)
		{
			return Insert(insertedResource, infoType, null);
		}

		/// <summary>
		/// 添加信息资源。
		/// </summary>
		/// <param name="insertedResource">添加的信息资源。</param>
		/// <param name="infoType">信息类别。</param>
		/// <param name="watermark">水印信息。</param>
		/// <returns>添加的信息资源。</returns>
		public static InfoResource Insert(InfoResource insertedResource, string infoType, InfoResourceWatermark watermark)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			InfoResource infoResource = insertedResource.ToNewEntity<InfoResource>();
			infoResource.CreatorId = Users.CurrentUserId;
			infoResource.CreationDate = DateTime.Now;
			infoResource.ResourceSize = infoResource.Content.Length;

			GetInsertRight(dc, infoResource, true);	// 权限验证

			// 注册信息项。
			InfoItems.Register(new InfoItem
			{
				GlobalId = infoResource.GlobalId,
				InfoType = infoType,
				IsPersistent = false
			});

			if (watermark != null && watermark.HasWatermark == true)
			{
				infoResource.InfoResourceWatermark = watermark;
			}
			dc.InfoResources.InsertOnSubmit(infoResource);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Insert, INFO_TYPE, infoResource.ResourceId.ToString(), PermissionCode.Insert.ToString(), infoResource.Name);

			// 返回只读数据
			return insertedResource.FillByEntity(infoResource);
		}

		/// <summary>
		/// 添加信息资源。
		/// </summary>
		/// <param name="insertedResource"></param>
		/// <param name="infoType"></param>
		/// <param name="watermark"></param>
		/// <param name="userName"></param>
		/// <returns></returns>
		public static InfoResource Insert(InfoResource insertedResource, string infoType, InfoResourceWatermark watermark,string userName)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			InfoResource infoResource = insertedResource.ToNewEntity<InfoResource>();
			infoResource.CreatorId = Users.GetUserId(userName);
			infoResource.CreationDate = DateTime.Now;
			infoResource.ResourceSize = infoResource.Content.Length;

			GetInsertRight(dc, infoResource,userName, true);	// 权限验证

			// 注册信息项。
			InfoItems.Register(new InfoItem
			{
				GlobalId = infoResource.GlobalId,
				InfoType = infoType,
				IsPersistent = false
			});

			if (watermark != null && watermark.HasWatermark == true)
			{
				infoResource.InfoResourceWatermark = watermark;
			}
			dc.InfoResources.InsertOnSubmit(infoResource);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Insert, INFO_TYPE, infoResource.ResourceId.ToString(), PermissionCode.Insert.ToString(), infoResource.Name);

			// 返回只读数据
			return insertedResource.FillByEntity(infoResource);
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除信息资源。
		/// </summary>
		/// <param name="resourceId">删除的信息资源标识 ResourceId。</param>
		public static void Delete(int resourceId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			InfoResource infoResource = dc.InfoResources.GetItem(resourceId);
			if (infoResource == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetDeleteRight(dc, infoResource, true);	// 权限验证

			Delete(dc, infoResource);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Delete, INFO_TYPE, infoResource.ResourceId.ToString(), PermissionCode.Delete.ToString(), infoResource.Name);
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, InfoResource infoResource)
		{
			InfoResourceCopies.DeleteByParent(dc, infoResource);	// 删除关联数据
			dc.InfoResources.DeleteOnSubmit(infoResource);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, InfoItem parent)
		{
			IEnumerable<InfoResource> e = parent.InfoResources.AsEnumerable();
			foreach (InfoResource i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}



	/// <summary>
	/// InfoResources 类对应操作权限部分。
	/// </summary>
	public static partial class InfoResources
	{
		#region GetInsertRight
		/// <summary>
		/// 获取当前用户是否有添加信息资源的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="infoResource">添加的信息资源对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有添加信息资源的权限。</returns>
		public static bool GetInsertRight(GenericModuleDataContext dc, InfoResource infoResource, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Insert;
			bool b = dc.AccessController.GetUserIsHasPermission<InfoResource>(userName, infoResource, ResourceId, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, infoResource, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有添加信息资源的权限。
		/// </summary>
		/// <param name="infoResource">添加的信息资源对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有添加信息资源的权限。</returns>
		public static bool GetInsertRight(InfoResource infoResource, bool triggerException)
		{
			return GetInsertRight(new GenericModuleDataContext(), infoResource, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有添加信息资源的权限。
		/// </summary>
		/// <param name="infoResource">添加的信息资源对象。</param>
		/// <returns>布尔值表示当前用户是否有添加信息资源的权限。</returns>
		public static bool GetInsertRight(InfoResource infoResource)
		{
			return GetInsertRight(infoResource, false);
		}
		/// <summary>
		/// 获取当前用户是否有添加信息资源的权限。
		/// </summary>
		/// <param name="dc"></param>
		/// <param name="infoResource"></param>
		/// <param name="userName"></param>
		/// <param name="triggerException"></param>
		/// <returns></returns>
		public static bool GetInsertRight(GenericModuleDataContext dc, InfoResource infoResource,string userName, bool triggerException)
		{			
			int permissionCode = (int)PermissionCode.Insert;
			bool b = dc.AccessController.GetUserIsHasPermission<InfoResource>(userName, infoResource, ResourceId, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, infoResource, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}
		/// <summary>
		/// 获取当前用户是否有添加信息资源的权限。
		/// </summary>
		/// <param name="infoResource"></param>
		/// <param name="userName"></param>
		/// <param name="triggerException"></param>
		/// <returns></returns>
		public static bool GetInsertRight(InfoResource infoResource, string userName, bool triggerException)
		{
			return GetInsertRight(new GenericModuleDataContext(), infoResource,userName, triggerException);
		}
		/// <summary>
		/// 获取当前用户是否有添加信息资源的权限。
		/// </summary>
		/// <param name="infoResource"></param>
		/// <param name="userName"></param>
		/// <returns></returns>
		public static bool GetInsertRight(InfoResource infoResource, string userName)
		{
			return GetInsertRight(infoResource,userName, false);
		}

		#endregion



		#region GetDeleteRight
		/// <summary>
		/// 获取当前用户是否有删除信息资源的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="infoResource">删除的信息资源对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除信息资源的权限。</returns>
		public static bool GetDeleteRight(GenericModuleDataContext dc, InfoResource infoResource, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Delete;
			bool b = dc.AccessController.GetUserIsHasPermission<InfoResource>(userName, infoResource, ResourceId, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, infoResource, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有删除信息资源的权限。
		/// </summary>
		/// <param name="infoResource">删除的信息资源对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除信息资源的权限。</returns>
		public static bool GetDeleteRight(InfoResource infoResource, bool triggerException)
		{
			return GetDeleteRight(new GenericModuleDataContext(), infoResource, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有删除信息资源的权限。
		/// </summary>
		/// <param name="infoResource">删除的信息资源对象。</param>
		/// <returns>布尔值表示当前用户是否有删除信息资源的权限。</returns>
		public static bool GetDeleteRight(InfoResource infoResource)
		{
			return GetDeleteRight(infoResource, false);
		}
		#endregion
	}



	/// <summary>
	/// InfoResources 类后台管理部分。
	/// </summary>
	public static partial class InfoResources
	{
		public static bool GetManageListRight()
		{
			return InfoItems.GetManageListRight();
		}



		public class InfoResourceBaseInfo
		{
			public int ResourceId { get; set; }
			public System.Guid GlobalId { get; set; }
			public string Name { get; set; }
			public string ResourceType { get; set; }
			public long ResourceSize { get; set; }
			public System.DateTime CreationDate { get; set; }
			public System.Nullable<System.Guid> CreatorId { get; set; }
		}



		public static PaginationInfo<List<InfoResourceBaseInfo>> GetPaginationManageList(Guid globalId, string keyword, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			var q = from p in dc.InfoResources
					where p.GlobalId == globalId
					select new InfoResourceBaseInfo { ResourceId = p.ResourceId, GlobalId = p.GlobalId, Name = p.Name, ResourceType = p.ResourceType, ResourceSize = p.ResourceSize, CreationDate = p.CreationDate, CreatorId = p.CreatorId };

			if (!string.IsNullOrEmpty(keyword))
				q = q.Where(p => p.Name.Contains(keyword));

			PaginationInfo<List<InfoResourceBaseInfo>> pageInfo = new PaginationInfo<List<InfoResourceBaseInfo>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q.Count();
			pageInfo.Records = q.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}
	}



	/// <summary>
	/// InfoResources 类对应前端数据读取部分。
	/// </summary>
	public static partial class InfoResources
	{
		public static InfoResourceBaseInfo GetBaseInfoItem(int resourceId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			var q = from p in dc.InfoResources
					where p.ResourceId == resourceId
					select new InfoResourceBaseInfo { ResourceId = p.ResourceId, GlobalId = p.GlobalId, Name = p.Name, ResourceType = p.ResourceType, ResourceSize = p.ResourceSize, CreationDate = p.CreationDate, CreatorId = p.CreatorId };
			return q.SingleOrDefault();
		}

		public static PaginationInfo<List<InfoResourceBaseInfo>> GetPaginationBaseInfoItems(Guid globalId, string keyword, string[] filterResourceTypes, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			var q = from p in dc.InfoResources
					where p.GlobalId == globalId
					select new InfoResourceBaseInfo { ResourceId = p.ResourceId, GlobalId = p.GlobalId, Name = p.Name, ResourceType = p.ResourceType, ResourceSize = p.ResourceSize, CreationDate = p.CreationDate, CreatorId = p.CreatorId };

			if (!string.IsNullOrEmpty(keyword))
				q = q.Where(p => p.Name.Contains(keyword));

			if (filterResourceTypes != null && filterResourceTypes.Length > 0)
				q = q.Where(p => filterResourceTypes.Contains(p.ResourceType));

			PaginationInfo<List<InfoResourceBaseInfo>> pageInfo = new PaginationInfo<List<InfoResourceBaseInfo>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q.Count();
			pageInfo.Records = q.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}
	}



	public partial class InfoResource
	{
		public InfoResourceCopy GetCopy(string copyName, IResourceCopyGenerator generator)
		{
			return Hxsoft.AppV1.Module.InfoResourceCopies.GenerateCopy(this, copyName, generator);
		}
	}
}